package com.runshu.newsmartark.util;


import com.runshu.newsmartark.util.common.LogUtil;

/**
 * ref解码工具
 */
public class RefUtil {
    public static void main(String[] args) {
        decryptRfid("12345678");
    }

    public static String decryptRfid(String param)
    {
        String decryStr = "";
        param = param.replace("\r\n", "").replace(" ", "");

        // 可能是支架等耗材本身带芯片，这种与rfid标签冲突，需要过滤掉
        if(param.length() > 30) {
            return null;
        }

        if (param.substring(0, 2).equals("EE"))
        {
            int interceptNum = Integer.parseInt(param.substring(2, 4));
            String keyStr = param.substring(param.length() - interceptNum * 2, param.length() - interceptNum * 2 + interceptNum * 2);

            for (int i = 0; i < keyStr.length(); i = i + 2)
            {
                String variable = keyStr.substring(i, i+2);
                variable = variable.substring(1, 2) + variable.substring(0, 1);
                int number = 0;
                try{
                    number = Integer.parseInt(variable, 8);
                }catch(Exception e) {
                    LogUtil.e("RFID解密","解码失败，有问题的rfid：{"+param+"}，variable：{"+  variable+"}");
                    return "#"+param;
                }
                number = (number * 7) % 10;
                decryStr = decryStr + number;
            }
            LogUtil.e("RFID解密","RefUtil解码之前的产品id：{"+param+"}，RefUtil解码之后的产品id：{"+  decryStr+"}");
            return decryStr;
        }else {
            LogUtil.e("RFID解密","not equals EE，产品id：{"+param+"}");
            try{
                param = String.valueOf(Long.parseLong(param));
            }catch (Exception e) {
                e.printStackTrace();
                LogUtil.e("RFID解密","不能转为long的rfid为,已被过滤,不会进行盘点====="+param);
                param = null;
            }
            LogUtil.e("RFID解密","not equals EE change，产品id：{}====="+param);
            return param;
        }

    }
}
